<?php

namespace User\Form\Fieldset;

use User\Model\Entity\User;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class ProfileFieldset extends Fieldset implements InputFilterProviderInterface {
    public function __construct() {
    	parent::__construct('user');
        $this->setHydrator(new ClassMethodsHydrator(false))
             ->setObject(new User());

      	$this->add(array(
            'name' => 'name',
            'attributes' => array(
                'type' => 'text',
      			'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'Name',
                'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label', 
        		),
            ),
        ));
        
      	$this->add(array(
            'name' => 'dateBirth',
            'attributes' => array(
                'type' => 'text',
      			'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'Date Birth',
                'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label', 
        		),
            ),
        ));
        
      	$this->add(array(
            'name' => 'cpf',
            'attributes' => array(
                'type' => 'text',
      			'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'CPF',
                'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label', 
        		),
            ),
        ));
      	
        $this->add(array(
            'name' => 'job',
            'attributes' => array(
                'type' => 'text',
      			'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'Job',
                'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label', 
        		),
            ),
        ));
        
        $this->add(array(
            'name' => 'phone1',
            'attributes' => array(
                'type' => 'text',
      			'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'Phone 1',
                'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label', 
        		),
            ),
        ));
        
        $this->add(array(
            'name' => 'phone2',
            'attributes' => array(
                'type' => 'text',
      			'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'Phone 2',
                'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label', 
        		),
            ),
        ));
        
        $this->add(array(
            'name' => 'biography',
            'attributes' => array(
                'type' => 'textarea',
        		'class' => 'form-control',
        		'rows' => 5,
            ),
            'options' => array(
                'label' => 'Biography',
                'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label', 
        		),
            ),
        ));
        
        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'addresses',
            'options' => array(
                'label' => 'Please choose categories for this product',
                'count' => 1,
                'should_create_template' => true,
                'allow_add' => true,
                'target_element' => array(
                    'type' => 'User\Form\Fieldset\AddressFieldset'
                )
            )
        ));
        
        $this->add(array(
        	'type' => 'Zend\Form\Element\Select',
            'name' => 'gender',
        	'attributes' => array(
      			'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'Gender',
            	'label_attributes' => array( 
            		'class' => 'col-lg-2 control-label',
        		),
        		'empty_option' => 'Select',
                'value_options' => array(
                    '0' => 'Male',
                    '1' => 'Female'
                ),
            ),
        ));
    }

    /**
     * Should return an array specification compatible with
     * {@link Zend\InputFilter\Factory::createInputFilter()}.
     *
     * @return array
     */
    public function getInputFilterSpecification() {
        return array(
            'name' => array(
                'required' => true,
                'filters'  => array(
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'min' => 3,
                            'max' => 100,
                        ),
                    ),
                ),
            ),
            'dateBirth' => array(
                'required' => false,
                'filters'  => array(
                    array('name' => 'Int'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'Date',
                        'options' => array(
                    		'format' => 'd/m/Y',
                        ),
                    ),
                ),
            ),
            'cpf' => array(
                'required' => false,
            	'filters'  => array(
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'Regex',
                    	'options'=>array(
				        	'messages' => array(
				        		'regexNotMatch' => 'The CPF are invalid or incorrect'
                    		),
				        	'pattern' => '/^(([0-9]{3}.[0-9]{3}.[0-9]{3}-[0-9]{2})|([0-9]{11}))$/',
				        ),
                    ),
                ),
            ),
            'phone1' => array(
                'required' => false,
                'filters'  => array(
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'Regex',
                    	'options'=>array(
				        	'messages' => array(
				        		'regexNotMatch' => 'The Phone are incorrect'
                    		),
				        	'pattern' => '/^((\([0-9]{2}\))\s([9]{1})?([0-9]{4})-([0-9]{4})|([0-9]{10})|([0-9]{11}))$/',
				        ),
                    ),
                ),
            ),
            'phone2' => array(
                'required' => false,
                'filters'  => array(
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'Regex',
                    	'options'=>array(
				        	'messages' => array(
				        		'regexNotMatch' => 'The Phone are incorrect'
                    		),
				        	'pattern' => '/^((\([0-9]{2}\))\s([9]{1})?([0-9]{4})-([0-9]{4})|([0-9]{10})|([0-9]{11}))$/',
				        ),
                    ),
                ),
            ),
        );
    }
}